<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言</title>
    <style lang="">
        li{
            margin: 5px;
            width: 300px;
            background:silver;
        }
        li a{
            float: right;
        }
    </style>
</head>

<body>
    <div>
        <textarea name="" id=""></textarea>
        <button>发布</button>
        <ul>
            
        </ul>
    </div>
    <script>
        var btn = document.querySelector('button');
        var text = document.querySelector('textarea');
        var ul = document.querySelector('ul');
        btn.onclick = function () {
            if (text.value == '') {
                alert('未输入内容');
                return false;
            } else {
                var new_li = document.createElement('li');
                new_li.innerHTML=text.value+"<a href='javascript:;'>删除</a>";
               // ul.appendChild(li);
                ul.insertBefore(new_li,ul.children[0]);
                var as=document.querySelectorAll('a');
                for(var i=0;i<as.length;i++)
                {
                    as[i].onclick=function(){
                        ul.removeChild(this.parentNode);
                    }
                }
            }

        }
    </script>
</body>

</html>